<?php
namespace Custom\Controller;

use Think\Controller;

class AuthController extends UserController
{
    public function __construct()
    {
        parent::__construct();
        $this->AuthModel = D('Store_auth');
    }

    public function index()
    {
//获取所有的管理员
        $this->Authlist = $this->AuthModel->select();
        $Authlist = $this->AuthModel->field('store_auth_id id,store_auth_name,store_auth_pid pid,store_auth_controller,store_auth_action')->where('store_is_menu=1')->select();
        $this->Authlist = getTree($Authlist);
        $this->display();
    }    //添加角色

    public function add()
    {
        if (IS_POST) {
            $data = $this->AuthModel->create();
            if (!$data) {
                $this->error('添加错误!');
                die;
            }
            $res = $this->AuthModel->add($data);
            if ($res) {
                $this->success('添加成功', U('index'), 2);
                die;
            } else {
                $this->error('添加失败,错误为:' . $this->AuthModel->getError());
                die;
            }
        }
        $this->topAuth = $this->AuthModel->where("store_auth_pid=0")->select();
        $this->display();
    }

//编辑角色
    public function edit()
    {
        $id = I('get.auth_id');
        $info = $this->AuthModel->select($id);
        $this->assign('info', $info);
        if (IS_POST) {
            $data['auth_id'] = I('post.auth_id');
            $data['auth_name'] = trim(I('auth_name'));
            $data['auth_controller'] = trim(I('auth_controller'));
            $data['auth_action'] = trim(I('auth_action'));
            $res = $this->AuthModel->save($data);
            $res ? $this->success('修改成功', U('index'), 1) : $this->error('修改失败');
        } else {
            $this->display();
        }
    }

//删除角色
    public function del()
    {
        $id = I('get.auth_id');
        $res = $this->AuthModel->delete($id);
        $res ? $this->success('删除成功', U('index'), 1) : $this->error('删除失败');
    }
}

